import MenuModel from '@/model/Menus'
import RoleModel from '@/model/Role'
class AdminController {
  async getMenu (ctx) {
    const data = await MenuModel.find()
    ctx.body = {
      code: 200,
      data,
      msg: 'success'
    }
  }

  async addMenu (ctx) {
    const { body } = ctx.request
    const menu = new MenuModel(body)
    await menu.save()
    ctx.body = {
      code: 200,
      data: menu,
      msg: 'success'
    }
  }

  async updateMenu (ctx) {
    const { body } = ctx.request
    const data = { ...body }
    delete data._id
    const res = await MenuModel.updateOne({ _id: body._id }, data)
    ctx.body = {
      data: res,
      code: 200,
      msg: 'success'
    }
  }

  async deleteMenu (ctx) {
    const { id } = ctx.params
    const res = await MenuModel.deleteOne({ _id: id })
    ctx.body = {
      code: 200,
      data: res,
      msg: 'success'
    }
  }

  async getRole (ctx) {
    const data = await RoleModel.find()
    ctx.body = {
      code: 200,
      data,
      msg: 'success'
    }
  }

  async addRole (ctx) {
    const { body } = ctx.request
    const menu = new RoleModel(body)
    await menu.save()
    ctx.body = {
      code: 200,
      data: menu,
      msg: 'success'
    }
  }

  async updateRole (ctx) {
    const { body } = ctx.request
    const data = { ...body }
    delete data._id
    const res = await RoleModel.updateOne({ _id: body._id }, data)
    ctx.body = {
      data: res,
      code: 200,
      msg: 'success'
    }
  }

  async deleteRole (ctx) {
    const { id } = ctx.params
    const res = await RoleModel.deleteOne({ _id: id })
    ctx.body = {
      code: 200,
      data: res,
      msg: 'success'
    }
  }
}

export default new AdminController()
